import 'package:flutter/material.dart';
import 'package:getx_scaffold/getx_scaffold.dart';
import 'package:oramemo/common/utils/time_util.dart';
import 'package:oramemo/entity/voice_record_transliterate_entity.dart';
import 'package:oramemo/common/langs/text_key.dart';

class BookmarkListBottomSheet extends StatelessWidget {
  final List<VoiceRecordTransliterateSegments> bookmarks;
  final Function(VoiceRecordTransliterateSegments) onTap;
  final Function(VoiceRecordTransliterateSegments) onDelete;

  const BookmarkListBottomSheet({
    Key? key,
    required this.bookmarks,
    required this.onTap,
    required this.onDelete,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      constraints: BoxConstraints(
        maxHeight: MediaQuery.of(context).size.height * 0.7,
      ),
      decoration: const BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.vertical(top: Radius.circular(16)),
      ),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          // 顶部标题栏
          Container(
            padding: const EdgeInsets.all(16),
            decoration: const BoxDecoration(
              border: Border(
                bottom: BorderSide(color: Colors.black12),
              ),
            ),
            child: Row(
              children: [
                Text(
                  TextKey.bookmarkListTitle.tr,
                  style: const TextStyle(
                    fontSize: 18,
                    fontWeight: FontWeight.bold,
                  ),
                ),
                const Spacer(),
                Text(
                  TextKey.bookmarkListCount.tr.replaceFirst('%d', bookmarks.length.toString()),
                  style: const TextStyle(
                    color: Colors.grey,
                  ),
                ),
              ],
            ),
          ),

          // 书签列表
          if (bookmarks.isEmpty)
            Padding(
              padding: const EdgeInsets.all(32),
              child: Text(
                TextKey.bookmarkListEmpty.tr,
                style: const TextStyle(color: Colors.grey),
              ),
            )
          else
            Flexible(
              child: ListView.builder(
                shrinkWrap: true,
                itemCount: bookmarks.length,
                itemBuilder: (context, index) {
                  final bookmark = bookmarks[index];
                  return Dismissible(
                    key: ValueKey(bookmark.id),
                    background: Container(
                      color: Colors.red,
                      alignment: Alignment.centerRight,
                      padding: const EdgeInsets.only(right: 16),
                      child: const Icon(
                        Icons.delete,
                        color: Colors.white,
                      ),
                    ),
                    direction: DismissDirection.endToStart,
                    onDismissed: (direction) {
                      onDelete(bookmark);
                    },
                    child: ListTile(
                      leading: const Icon(
                        Icons.bookmark,
                        color: Colors.orange,
                      ),
                      title: Text(
                        bookmark.text ?? '',
                        maxLines: 2,
                        overflow: TextOverflow.ellipsis,
                      ),
                      subtitle: Text(
                        _formatDuration(bookmark.start!),
                        style: const TextStyle(color: Colors.grey),
                      ),
                      onTap: () {
                        Navigator.pop(context);
                        onTap(bookmark);
                      },
                    ),
                  );
                },
              ),
            ),
        ],
      ),
    );
  }

  String _formatDuration(String duration) {
    final start = (duration.toDouble() ?? 0) * 1000;
    return TimeUtils.formatDuration(Duration(milliseconds: start.toInt()));
  }
} 